﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

using Effects.GUI;
using Effects.Effects;

namespace Effects.Application
{
    /// <summary>
    ///  Simple Form used to choose which effect will be added to the effect board.
    /// </summary>
    /// <remarks>
    ///  This Form shows list of availible effects and lets user to choose one of them. This Effect will be added to the effect boards.
    /// </remarks>
    public partial class AddEffectForm : Form
    {
        /// <summary>
        ///  Types of effects that can be created
        /// </summary>
        private Type[] effects;
        /// <summary>
        ///  Gets or sets availible effects.
        /// </summary>
        public Type[] Effects
        {
            get { return effects; }
            set 
            { 
                effects = value;
                SetItems();
            }
        }

        /// <summary>
        /// <see cref="EffectLibrary"/> of availible effects. It is used to create new instances of effects.
        /// </summary>
        private global::Effects.GUI.EffectLibrary library;

        /// <summary>
        ///  Gets or sets <see cref="EffectLibrary"/> that is used to create instances of new effects.
        /// </summary>
        public global::Effects.GUI.EffectLibrary Library
        {
            get { return library; }
            set 
            {
                library = value;
                SetItems();
            }
        }
        
        /// <summary>
        ///  Creates new instance with empty <see cref="EffectLibrary"/>.
        /// </summary>
        public AddEffectForm()
        {
            InitializeComponent();
            this.Channel1RadioButton.Checked = true;
        }
        /// <summary>
        ///  Cancel button click event. Closes the form.
        /// </summary>
        /// <param name="sender"> Object that has raised the event == Cancel button </param>
        /// <param name="e"> Event arguments. </param>
        private void CancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        ///  Event raised when effect is confirmed to be added.
        /// </summary>
        public event AddEffectEventHandler EffectToAdd;

        /// <summary>
        ///  Add button click event handler.
        /// </summary>
        /// <param name="sender"> Object that has raised the event == Add button </param>
        /// <param name="e"> Event arguments. </param>
        /// <remarks>
        ///  Method finds choosen effect and channel and raises EffectToAdd event.
        /// </remarks>
        private void AddButton_Click(object sender, EventArgs e)
        {
            Effect eff = null;
            string selected = (string)EffectListBox.SelectedItem;

            // create new instance of selected effect
            eff = library.GetEffect(selected);

            // choose channel
            int channel;
            if (Channel1RadioButton.Checked)
                channel = 0;
            else
                channel = 1;

            // raise the event
            AddEffectEventArgs args = new AddEffectEventArgs(eff,channel);
            try
            {
                EffectToAdd(this, args);
            }
            catch (NullReferenceException)
            { }

            this.Close();
        }

        /// <summary>
        ///  Sets effect Names from Library as items of EffectListBox. 
        /// </summary>
        private void SetItems()
        {
            EffectListBox.Items.Clear();
            string[] names = library.Names;
            foreach (string name in names)
                EffectListBox.Items.Add(name);
        }
    }
}
